package com.matthewmadson.util.configurator;

import com.google.common.base.Joiner;

public class Location
implements Comparable<Location>
{
	private String[] location = new String[2];
	private int priority;
	private LocationType type;
	
	public Location(final String location, final int priority, final LocationType type)
	{
		this(location,"",priority,type);
	}
	
	public Location(final String packageName, final String fileName, final int priority, final LocationType type)
	{
		this.location[0] = packageName;
		this.location[1] = fileName;
		this.priority = priority;
		this.setType(type);
	}
	
	public String getExternalLocation()
	{
		return location[0];
	}
	
	public String getPackageLocation()
	{
		return location[0];
	}
	
	public String getInternalFileName()
	{
		return location[1];
	}
	
	public String getCommandLineFlag()
	{
		return location[0];
	}

	public int compareTo(Location other)
	{
		if(other == null)
			throw new NullPointerException();
		if(this.priority < other.priority)
			return -1;
		if(this.priority == other.priority)
			return 0;
		if(this.priority > other.priority)
			return 1;
		return 0;
	}


	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + priority;
		result = prime * result + ((getType() == null) ? 0 : getType().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if(this == obj)
			return true;
		if(obj == null)
			return false;
		if(getClass() != obj.getClass())
			return false;
		Location other = (Location) obj;
		if(priority != other.priority)
			return false;
		if(getType() == null)
		{
			if(other.getType() != null)
				return false;
		}
		else if(!getType().equals(other.getType()))
			return false;
		return true;
	}

	@Override
	public String toString()
	{
		return this.type+": "+Joiner.on(" ").join(this.location);
	}

	private void setType(LocationType type)
	{
		this.type = type;
	}

	public LocationType getType()
	{
		return type;
	}
}
